package cmd

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/vuecmf/vuecmf-go/v3/app"
	"github.com/vuecmf/vuecmf-go/v3/app/vuecmf/model"
	"gorm.io/gorm"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
	"time"
)

// Migrator 数据库迁移
func Migrator(aType string, version int) (string, error) {
	var err error
	var printTxt string
	switch aType {
	case "init":
		err = initDb()
		printTxt = "数据库初始化"
	case "up":
		err = up()
		printTxt = "数据库升级"
	case "down":
		err = down(version)
		printTxt = "数据库回滚"
	default:
		err = errors.New("不支持的选项类型！仅支持init|up|down")
	}

	return printTxt, err
}

// initDb 数据库初始化
func initDb() error {
	env := app.Config().Env
	db := app.Connect(env)

	//先检查迁移表是否存在
	dbConf := app.DbConf()
	dbCfg, ok := dbConf.Connect[env]
	if ok == false {
		return errors.New("数据库配置（" + env + "）不存在")
	}

	migrationName := "Init database"
	migrateTable := db.NamingStrategy.TableName("migrations")
	var checkRes string
	db.Raw("select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = ? and TABLE_NAME = ?", dbCfg.Database, migrateTable).Scan(&checkRes)

	if checkRes != "" {
		var ver int

		db.Table(migrateTable).Select("version").
			Where("migration_name = ?", migrationName).Find(&ver)

		if ver > 0 {
			return errors.New("数据库已经执行过初始化操作")
		}
	}

	startTime := time.Now()

	//创建管理员表
	if err := createTable(db, "admin", &model.Admin{}, "系统--管理员表"); err != nil {
		return err
	}
	//创建字段的选项表
	if err := createTable(db, "field_option", &model.FieldOption{}, "系统--字段的选项列表"); err != nil {
		return err
	}
	//创建菜单表
	if err := createTable(db, "menu", &model.Menu{}, "系统--菜单表"); err != nil {
		return err
	}
	//创建模型动作表
	if err := createTable(db, "model_action", &model.ModelAction{}, "系统--模型动作表"); err != nil {
		return err
	}
	//创建模型配置管理表
	if err := createTable(db, "model_config", &model.ModelConfig{}, "系统--模型配置管理表"); err != nil {
		return err
	}
	//创建模型字段管理表
	if err := createTable(db, "model_field", &model.ModelField{}, "系统--模型字段管理表"); err != nil {
		return err
	}
	//创建模型表单表
	if err := createTable(db, "model_form", &model.ModelForm{}, "系统--模型表单表"); err != nil {
		return err
	}
	//创建模型表单联动设置表
	if err := createTable(db, "model_form_linkage", &model.ModelFormLinkage{}, "系统--模型表单联动设置表"); err != nil {
		return err
	}
	//创建模型表单验证设置表
	if err := createTable(db, "model_form_rules", &model.ModelFormRules{}, "系统--模型表单验证设置表"); err != nil {
		return err
	}
	//创建模型索引设置表
	if err := createTable(db, "model_index", &model.ModelIndex{}, "系统--模型索引设置表"); err != nil {
		return err
	}
	//创建模型关联设置表
	if err := createTable(db, "model_relation", &model.ModelRelation{}, "系统--模型关联设置表"); err != nil {
		return err
	}
	//创建角色表
	if err := createTable(db, "roles", &model.Roles{}, "系统--角色表"); err != nil {
		return err
	}
	//创建角色表
	if err := createTable(db, "rules", &model.Rules{}, "系统--授权规则表"); err != nil {
		return err
	}
	//创建数据库升级日志表
	if err := createTable(db, "migrations", &model.Migrations{}, "系统--数据库升级日志表"); err != nil {
		return err
	}
	//创建应用管理表
	if err := createTable(db, "app_config", &model.AppConfig{}, "系统--应用配置表"); err != nil {
		return err
	}

	//写入初始数据
	tableName := db.NamingStrategy.TableName("app_config")
	query := fmt.Sprintf(`INSERT INTO %s(id, app_name, login_enable, auth_enable, exclusion_url, type, status) VALUES 
    (1, 'vuecmf', 10, 10, '/vuecmf,/vuecmf/index/success,/vuecmf/index/fail,/vuecmf/index/index,/vuecmf/admin/login, /vuecmf/admin/logout, /vuecmf/model_action/get_api_map', 10, 10),
    (2, 'home', 20, 20, '', 20, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_config")
	query = fmt.Sprintf(`INSERT INTO %s(id, app_id, table_name, label, component_tpl, default_action_id, search_field_id, type, is_tree, remark, status) VALUES 
    (1, 1, 'model_config', '模型配置', 'template/content/List', 1, '3,4,5', 10, 20, '系统--模型配置管理表', 10),
	(2, 1, 'model_action', '模型动作', 'template/content/List', 20, '21,22,24', 10, 20, '系统--模型动作表', 10),
	(3, 1, 'model_field', '模型字段', 'template/content/List', 40, '41,42,44', 10, 20, '系统--模型字段管理表', 10),
	(4, 1, 'field_option', '字段选项', 'template/content/List', 60, '64,65', 10, 20, '系统--字段的选项列表', 10),
	(5, 1, 'model_index', '模型索引', 'template/content/List', 80, '83', 10, 20, '系统--模型索引设置表', 10),
	(6, 1, 'model_relation', '模型关联', 'template/content/List', 100, '102,104', 10, 20, '系统--模型关联设置表', 10),
	(7, 1, 'menu', '菜单', 'template/content/List', 120, '121,122,126', 10, 10, '系统--菜单表', 10),
	(8, 1, 'admin', '管理员', 'template/content/List', 140, '141,143,144', 10, 20, '系统--管理员表', 10),
	(9, 1, 'model_form', '模型表单', 'template/content/List', 180, '163,164', 10, 20, '系统--模型表单设置表', 10),
	(10, 1, 'model_form_rules', '模型表单验证', 'template/content/List', 200, '183,184,185', 10, 20, '系统--模型表单验证设置表', 10),
	(11, 1, 'roles', '角色', 'template/content/List', 220, '201', 10, 10, '系统--角色表', 10),
	(12, 1, 'model_form_linkage', '模型表单联动', 'template/content/List', 260, '221,222,223', 10, 20, '系统--模型表单联动设置表', 10),
	(13, 1, 'upload_file', '文件上传', '', 0, '', 10, 20, '系统--文件上传', 10),
	(14, 1, 'app_config', '应用配置', 'template/content/List', 300, '241,244', 10, 20, '系统--应用配置表', 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_field")
	query = fmt.Sprintf(`INSERT INTO %s(id, field_name, label, model_id, type, length, decimal_length, is_null, note, default_value, is_auto_increment, is_label, is_signed, is_show, is_fixed, column_width, is_filter, is_code, sort_num, status) VALUES 
	(1, 'id', 'ID', 1, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 1, 10),
	(2, 'app_id', '所属应用', 1, 'int', 11, 0, 20, '所属应用ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 2, 10),
	(3, 'table_name', '表名', 1, 'varchar', 64, 0, 20, '模型对应的表名(不含表前缘)', '', 20, 10, 20, 10, 20, 150, 10, 20, 3, 10),
	(4, 'label', '模型标签', 1, 'varchar', 64, 0, 20, '模型标签', '', 20, 10, 20, 10, 20, 150, 10, 20, 4, 10),
	(5, 'component_tpl', '组件模板', 1, 'varchar', 255, 0, 20, '组件模板', '', 20, 20, 20, 10, 20, 150, 10, 20, 5, 10),
	(6, 'default_action_id', '默认动作', 1, 'int', 11, 0, 20, '默认动作ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 6, 10),
	(7, 'search_field_id', '搜索字段', 1, 'varchar', 255, 0, 20, '搜索字段ID，多个用逗号分隔', '', 20, 20, 20, 10, 20, 300, 20, 20, 7, 10),
	(8, 'type', '类型', 1, 'smallint', 4, 0, 20, '类型：10=内置，20=扩展', '20', 20, 20, 20, 10, 20, 100, 10, 20, 8, 10),
	(9, 'is_tree', '目录树', 1, 'smallint', 4, 0, 20, '是否为目录树：10=是，20=否', '20', 20, 20, 20, 10, 20, 100, 10, 20, 9, 10),
	(10, 'remark', '表备注', 1, 'varchar', 255, 0, 20, '模型对应表的备注', '', 20, 20, 20, 10, 20, 100, 10, 20, 10, 10),
	(11, 'status', '状态', 1, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 11, 10),
	(20, 'id', 'ID', 2, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 12, 10),
	(21, 'label', '动作标签', 2, 'varchar', 64, 0, 20, '动作标签', '', 20, 10, 20, 10, 20, 150, 10, 20, 13, 10),
	(22, 'api_path', '后端请求地址', 2, 'varchar', 255, 0, 20, '后端请求地址', '', 20, 20, 20, 10, 20, 150, 10, 20, 14, 10),
	(23, 'model_id', '所属模型', 2, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 15, 10),
	(24, 'action_type', '动作类型', 2, 'varchar', 32, 0, 20, '动作类型', '', 20, 20, 20, 10, 20, 150, 10, 20, 16, 10),
	(25, 'status', '状态', 2, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 17, 10),
	(40, 'id', 'ID', 3, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 18, 10),
	(41, 'field_name', '字段名称', 3, 'varchar', 64, 0, 20, '表的字段名称', '', 20, 10, 20, 10, 20, 150, 10, 20, 19, 10),
	(42, 'label', '字段中文名', 3, 'varchar', 64, 0, 20, '表的字段中文名称', '', 20, 10, 20, 10, 20, 150, 10, 20, 20, 10),
	(43, 'model_id', '所属模型', 3, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 21, 10),
	(44, 'type', '字段类型', 3, 'varchar', 20, 0, 20, '表的字段类型', '', 20, 20, 20, 10, 20, 150, 10, 20, 22, 10),
	(45, 'length', '字段长度', 3, 'int', 11, 0, 20, '表的字段长度', '0', 20, 20, 20, 10, 20, 100, 10, 20, 23, 10),
	(46, 'decimal_length', '小数位数', 3, 'smallint', 2, 0, 20, '表的字段为decimal类型时的小数位数', '0', 20, 20, 20, 10, 20, 100, 10, 20, 24, 10),
	(47, 'is_null', '是否为空', 3, 'smallint', 4, 0, 20, '是否为空：10=是，20=否', '10', 20, 20, 20, 10, 20, 100, 10, 20, 25, 10),
	(48, 'note', '字段备注', 3, 'varchar', 255, 0, 20, '表的字段备注说明', '', 20, 20, 20, 10, 20, 150, 10, 20, 26, 10),
	(49, 'default_value', '默认值', 3, 'varchar', 255, 0, 20, '数据默认值', '', 20, 20, 20, 10, 20, 150, 10, 20, 27, 10),
	(50, 'is_auto_increment', '自动递增', 3, 'smallint', 4, 0, 20, '是否自动递增：10=是，20=否', '20', 20, 20, 20, 10, 20, 100, 10, 20, 28, 10),
	(51, 'is_label', '标题字段', 3, 'smallint', 4, 0, 20, '是否为标题字段：10=是，20=否', '20', 20, 20, 20, 10, 20, 100, 10, 20, 29, 10),
	(52, 'is_signed', '可为负数', 3, 'smallint', 4, 0, 20, '是否可为负数：10=是，20=否', '10', 20, 20, 20, 10, 20, 100, 10, 20, 30, 10),
	(53, 'is_show', '列表可显', 3, 'smallint', 4, 0, 20, '默认列表中显示：10=显示，20=不显示', '10', 20, 20, 20, 10, 20, 100, 10, 20, 31, 10),
	(54, 'is_fixed', '固定列', 3, 'smallint', 4, 0, 20, '默认列表中固定：10=固定，20=不固定', '20', 20, 20, 20, 10, 20, 100, 10, 20, 32, 10),
	(55, 'column_width', '列宽度', 3, 'int', 11, 0, 20, '列表中默认显示宽度：0表示不限', '150', 20, 20, 20, 10, 20, 100, 10, 20, 33, 10),
	(56, 'is_filter', '可筛选', 3, 'smallint', 4, 0, 20, '列表中是否可为筛选条件：10=是，20=否', '10', 20, 20, 20, 10, 20, 100, 10, 20, 34, 10),
	(57, 'sort_num', '排序', 3, 'int', 11, 0, 20, '表单/列表中字段的排列顺序(小在前)', '0', 20, 20, 20, 10, 20, 100, 10, 20, 36, 10),
	(58, 'status', '状态', 3, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 37, 10),
	(60, 'id', 'ID', 4, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 37, 10),
	(61, 'type', '类型', 4, 'smallint', 4, 0, 20, '类型：10=内置，20=扩展', '20', 20, 20, 20, 10, 20, 100, 10, 20, 38, 10),
	(62, 'model_id', '所属模型', 4, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 39, 10),
	(63, 'model_field_id', '模型字段', 4, 'int', 11, 0, 20, '模型字段ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 40, 10),
	(64, 'option_value', '选项值', 4, 'varchar', 64, 0, 20, '选项值', '', 20, 10, 20, 10, 20, 150, 10, 20, 41, 10),
	(65, 'option_label', '选项标签', 4, 'varchar', 255, 0, 20, '选项标签', '', 20, 20, 20, 10, 20, 150, 10, 20, 42, 10),
	(66, 'status', '状态', 4, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 43, 10),
	(80, 'id', 'ID', 5, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 44, 10),
	(81, 'model_id', '所属模型', 5, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 45, 10),
	(82, 'model_field_id', '模型字段', 5, 'varchar', 100, 0, 20, '模型字段ID', '', 20, 20, 20, 10, 20, 150, 10, 20, 46, 10),
	(83, 'index_type', '索引类型', 5, 'varchar', 32, 0, 20, '索引类型： PRIMARY=主键，NORMAL=常规，UNIQUE=唯一，FULLTEXT=全文', 'NORMAL', 20, 10, 20, 10, 20, 150, 10, 20, 47, 10),
	(84, 'status', '状态', 5, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 48, 10),
	(100, 'id', 'ID', 6, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 49, 10),
	(101, 'model_id', '所属模型', 6, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 50, 10),
	(102, 'model_field_id', '模型字段', 6, 'int', 11, 0, 20, '模型字段ID', '0', 20, 10, 20, 10, 20, 150, 20, 20, 51, 10),
	(103, 'relation_model_id', '关联模型', 6, 'int', 11, 0, 20, '关联模型ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 52, 10),
	(104, 'relation_field_id', '关联模型字段', 6, 'int', 11, 0, 20, '关联模型字段ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 53, 10),
	(105, 'relation_show_field_id', '显示字段', 6, 'varchar', 255, 0, 20, '关联模型显示字段ID,多个逗号分隔，全部用*', '', 20, 20, 20, 10, 20, 150, 10, 20, 54, 10),
	(106, 'relation_filter', '关联过滤条件', 6, 'varchar', 255, 0, 20, '关联过滤条件', '', 20, 20, 20, 10, 20, 150, 10, 20, 55, 10),
	(107, 'status', '状态', 6, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 56, 10),
	(120, 'id', 'ID', 7, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 20, 20, 150, 20, 20, 56, 10),
	(121, 'title', '菜单标题', 7, 'varchar', 64, 0, 20, '菜单标题', '', 20, 10, 20, 10, 20, 150, 10, 20, 57, 10),
	(122, 'icon', '菜单图标', 7, 'varchar', 32, 0, 20, '菜单图标', '', 20, 20, 20, 10, 20, 150, 10, 20, 58, 10),
	(123, 'pid', '父级', 7, 'int', 11, 0, 20, '父级ID', '0', 20, 20, 20, 20, 20, 150, 10, 20, 59, 10),
	(124, 'app_id', '应用', 7, 'int', 11, 0, 20, '应用ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 60, 10),
	(125, 'id_path', 'ID路径', 7, 'varchar', 255, 0, 20, 'ID路径,英文逗号分隔', '', 20, 20, 20, 10, 20, 150, 20, 20, 61, 10),
	(126, 'path_name', '菜单路径', 7, 'varchar', 255, 0, 20, '菜单路径,英文逗号分隔', '', 20, 20, 20, 10, 20, 150, 20, 20, 62, 10),
	(127, 'model_id', '模型', 7, 'int', 11, 0, 20, '模型ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 63, 10),
	(128, 'type', '类型', 7, 'smallint', 4, 0, 20, '类型：10=内置，20=扩展', '20', 20, 20, 20, 10, 20, 100, 10, 20, 64, 10),
	(129, 'sort_num', '排序', 7, 'int', 11, 0, 20, '菜单的排列顺序(小在前)', '0', 20, 20, 20, 10, 20, 150, 10, 20, 65, 10),
	(130, 'status', '状态', 7, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 150, 10, 20, 66, 10),
	(140, 'id', 'ID', 8, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 67, 10),
	(141, 'username', '用户名', 8, 'varchar', 32, 0, 20, '用户名', '', 20, 10, 20, 10, 20, 150, 10, 20, 68, 10),
	(142, 'password', '密码', 8, 'varchar', 255, 0, 20, '密码', '', 20, 20, 20, 20, 20, 150, 20, 20, 69, 10),
	(143, 'email', '邮箱', 8, 'varchar', 64, 0, 20, '邮箱', '', 20, 20, 20, 10, 20, 150, 10, 20, 70, 10),
	(144, 'mobile', '手机', 8, 'varchar', 32, 0, 20, '手机', '', 20, 20, 20, 10, 20, 150, 10, 20, 71, 10),
	(145, 'is_super', '超级管理员', 8, 'smallint', 4, 0, 20, '超级管理员：10=是，20=否', '20', 20, 20, 20, 10, 20, 150, 10, 20, 72, 10),
	(146, 'reg_time', '注册时间', 8, 'timestamp', 0, 0, 20, '注册时间', 'CURRENT_TIMESTAMP', 20, 20, 20, 10, 20, 150, 10, 20, 73, 10),
	(147, 'reg_ip', '注册IP', 8, 'varchar', 24, 0, 20, '注册IP', '', 20, 20, 20, 10, 20, 150, 20, 20, 74, 10),
	(148, 'last_login_time', '最后登录时间', 8, 'timestamp', 0, 0, 20, '最后登录时间', 'CURRENT_TIMESTAMP', 20, 20, 20, 10, 20, 150, 10, 20, 75, 10),
	(149, 'last_login_ip', '最后登录IP', 8, 'varchar', 24, 0, 20, '最后登录IP', '', 20, 20, 20, 10, 20, 150, 20, 20, 76, 10),
	(150, 'update_time', '更新时间', 8, 'timestamp', 0, 0, 20, '更新时间', 'CURRENT_TIMESTAMP', 20, 20, 20, 10, 20, 150, 10, 20, 77, 10),
	(151, 'token', '访问token', 8, 'varchar', 255, 0, 20, 'api访问token', '', 20, 20, 20, 10, 20, 150, 10, 20, 78, 10),
	(152, 'status', '状态', 8, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 79, 10),
	(160, 'id', 'ID', 9, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 80, 10),
	(161, 'model_id', '所属模型', 9, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 81, 10),
	(162, 'model_field_id', '模型字段', 9, 'int', 11, 0, 20, '模型字段ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 82, 10),
	(163, 'type', '控件类型', 9, 'varchar', 32, 0, 20, '表单控件类型', '', 20, 10, 20, 10, 20, 150, 10, 20, 83, 10),
	(164, 'default_value', '控件默认值', 9, 'varchar', 255, 0, 20, '表单控件默认值', '', 20, 20, 20, 10, 20, 150, 10, 20, 84, 10),
	(165, 'is_disabled', '是否禁用', 9, 'smallint', 4, 0, 20, '添加/编辑表单中是否禁用： 10=是，20=否', '20', 20, 20, 20, 10, 20, 100, 10, 20, 85, 10),
	(166, 'sort_num', '排序', 9, 'int', 11, 0, 20, '菜单的排列顺序(小在前)', '0', 20, 20, 20, 10, 20, 100, 10, 20, 86, 10),
	(167, 'status', '状态', 9, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 87, 10),
	(180, 'id', 'ID', 10, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 88, 10),
	(181, 'model_id', '所属模型', 10, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 89, 10),
	(182, 'model_form_id', '模型表单', 10, 'int', 11, 0, 20, '模型表单ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 90, 10),
	(183, 'rule_type', '验证类型', 10, 'varchar', 32, 0, 20, '表单验证类型', '', 20, 10, 20, 10, 20, 150, 10, 20, 91, 10),
	(184, 'rule_value', '验证规则', 10, 'varchar', 255, 0, 20, '表单验证规则', '', 20, 20, 20, 10, 20, 150, 10, 20, 92, 10),
	(185, 'error_tips', '错误提示', 10, 'varchar', 255, 0, 20, '表单验证不通过的错误提示信息', '', 20, 20, 20, 10, 20, 150, 10, 20, 93, 10),
	(186, 'status', '状态', 10, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 94, 10),
	(200, 'id', 'ID', 11, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 95, 10),
	(201, 'role_name', '角色名称', 11, 'varchar', 64, 0, 20, '用户的角色名称', '', 20, 10, 20, 10, 20, 150, 10, 20, 96, 10),
	(203, 'pid', '父级', 11, 'int', 11, 0, 20, '父级ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 98, 10),
	(204, 'id_path', '层级路径', 11, 'varchar', 255, 0, 20, '角色ID层级路径', '', 20, 20, 20, 10, 20, 150, 10, 20, 99, 10),
	(205, 'remark', '备注', 11, 'varchar', 255, 0, 20, '角色的备注信息', '', 20, 20, 20, 10, 20, 150, 10, 20, 100, 10),
	(206, 'status', '状态', 11, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 101, 10),
	(220, 'id', 'ID', 12, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 102, 10),
	(221, 'model_id', '所属模型', 12, 'int', 11, 0, 20, '所属模型ID', '0', 20, 20, 20, 10, 20, 150, 20, 20, 103, 10),
	(222, 'model_field_id', '模型字段', 12, 'int', 11, 0, 20, '模型字段ID', '0', 20, 10, 20, 10, 20, 150, 20, 20, 104, 10),
	(223, 'linkage_field_id', '联动字段', 12, 'int', 11, 0, 20, '联动表单的字段ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 105, 10),
	(224, 'linkage_action_id', '联动动作', 12, 'int', 11, 0, 20, '获取联动表单数据的动作ID', '0', 20, 20, 20, 10, 20, 150, 10, 20, 106, 10),
	(225, 'status', '状态', 12, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 107, 10),
	(240, 'id', 'ID', 14, 'int', 11, 0, 20, '自增ID', '0', 10, 20, 20, 10, 20, 100, 20, 20, 109, 10),
	(241, 'app_name', '应用名称', 14, 'varchar', 64, 0, 20, '应用名称', '', 20, 10, 20, 10, 20, 150, 10, 20, 110, 10),
	(242, 'login_enable', '登录验证', 14, 'smallint', 4, 0, 20, '是否开启登录验证, 10=是，20=否', '10', 20, 20, 20, 10, 20, 150, 10, 20, 111, 10),
	(243, 'auth_enable', '权限验证', 14, 'smallint', 4, 0, 20, '是否开启权限验证, 10=是，20=否', '10', 20, 20, 20, 10, 20, 150, 10, 20, 112, 10),
	(244, 'exclusion_url', '排除验证URL', 14, 'varchar', 2000, 0, 20, '排除验证的URL，多个用英文逗号分隔', '', 20, 20, 20, 10, 20, 150, 10, 20, 113, 10),
	(245, 'type', '类型', 14, 'smallint', 4, 0, 20, '类型：10=内置，20=扩展', '20', 20, 20, 20, 10, 20, 150, 10, 20, 114, 10),
	(246, 'status', '状态', 14, 'smallint', 4, 0, 20, '状态：10=开启，20=禁用', '10', 20, 20, 20, 10, 20, 100, 10, 20, 115, 10),
	(247, 'is_code', '显示源码', 3, 'smallint', 4, 0, 20, '是否显示文本源码，10=是，20=否', '20', 20, 20, 20, 10, 20, 100, 10, 20, 34, 10),
	(260, 'pid', '父级用户', 8, 'int', 11, 0, 20, '父级用户ID', '0', 20, 20, 20, 20, 20, 100, 20, 20, 0, 10),
	(261, 'placeholder', '表单提示', 9, 'varchar', 255, 0, 20, '表单提示信息', '', 20, 20, 20, 10, 20, 150, 20, 20, 88, 10),
	(262, 'is_edit', '可编辑', 9, 'smallint', 4, 0, 20, '可编辑：10=是，20=否', '10', 20, 20, 20, 10, 20, 100, 10, 20, 89, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_index")
	query = fmt.Sprintf(`INSERT INTO %s(id, model_id, model_field_id, index_type, status) VALUES 
	(1, 1, '3', 'UNIQUE', 10),
	(2, 2, '23,24', 'UNIQUE', 10),
	(3, 3, '41,43', 'UNIQUE', 10),
	(4, 4, '63,64', 'UNIQUE', 10),
	(5, 6, '102,104', 'UNIQUE', 10),
	(6, 8, '141', 'UNIQUE', 10),
	(7, 8, '143', 'UNIQUE', 10),
	(8, 8, '144', 'UNIQUE', 10),
	(9, 9, '162', 'UNIQUE', 10),
	(10, 11, '201', 'UNIQUE', 10),
	(11, 12, '222,223', 'UNIQUE', 10),
	(12, 14, '241', 'UNIQUE', 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("field_option")
	query = fmt.Sprintf(`INSERT INTO %s(id, model_id, model_field_id, option_value, option_label, type, status) VALUES 
	(1, 1, 5, 'template/content/List', '列表组件', 10, 10),
	(2, 1, 8, '10', '内置', 10, 10),
	(3, 1, 8, '20', '扩展', 10, 10),
	(4, 1, 9, '10', '是', 10, 10),
	(5, 1, 9, '20', '否', 10, 10),
	(6, 1, 11, '10', '开启', 10, 10),
	(7, 1, 11, '20', '禁用', 10, 10),
	(8, 2, 24, 'action_list', '获取动作列表', 10, 10),
	(9, 2, 24, 'list', '列表', 10, 10),
	(10, 2, 24, 'detail', '详情', 10, 10),
	(11, 2, 24, 'save', '保存', 10, 10),
	(12, 2, 24, 'save_all', '批量保存', 10, 10),
	(13, 2, 24, 'delete', '删除', 10, 10),
	(14, 2, 24, 'delete_batch', '批量删除', 10, 10),
	(15, 2, 24, 'dropdown', '下拉列表', 10, 10),
	(16, 2, 24, 'add_permission', '设置角色权限', 10, 10),
	(17, 2, 24, 'assign_role', '分配角色', 10, 10),
	(18, 2, 24, 'assign_users', '批量分配用户', 10, 10),
	(19, 2, 24, 'del_permission', '删除角色权限', 10, 10),
	(20, 2, 24, 'del_users', '批量删除用户', 10, 10),
	(21, 2, 24, 'get_all_roles', '获取所有角色', 10, 10),
	(22, 2, 24, 'get_all_users', '获取所有用户', 10, 10),
	(23, 2, 24, 'get_permission', '获取角色下所有权限', 10, 10),
	(24, 2, 24, 'get_roles', '获取用户的角色', 10, 10),
	(25, 2, 24, 'get_users', '获取角色下所有用户', 10, 10),
	(26, 2, 24, 'get_user_permission', '获取用户权限', 10, 10),
	(27, 2, 24, 'set_user_permission', '设置用户权限', 10, 10),
	(28, 2, 24, 'login', '登录后台', 10, 10),
	(29, 2, 24, 'logout', '退出系统', 10, 10),
	(30, 2, 24, 'nav', '导航菜单', 10, 10),
	(31, 2, 24, 'upload', '上传文件', 10, 10),
	(32, 2, 25, '10', '开启', 10, 10),
	(33, 2, 25, '20', '禁用', 10, 10),
	(34, 3, 44, 'char', '固定长度字符串', 10, 10),
	(35, 3, 44, 'varchar', '可变长度字符串', 10, 10),
	(36, 3, 44, 'text', '多行文本', 10, 10),
	(37, 3, 44, 'mediumtext', '中型多行文本', 10, 10),
	(38, 3, 44, 'longtext', '大型多行文本', 10, 10),
	(39, 3, 44, 'tinyint', '小型数值', 10, 10),
	(40, 3, 44, 'smallint', '中型数值', 10, 10),
	(41, 3, 44, 'int', '大型数值', 10, 10),
	(42, 3, 44, 'bigint', '越大型数值', 10, 10),
	(43, 3, 44, 'float', '单精度浮点型', 10, 10),
	(44, 3, 44, 'double', '双精度浮点型', 10, 10),
	(45, 3, 44, 'decimal', '金额型', 10, 10),
	(46, 3, 44, 'date', '日期', 10, 10),
	(47, 3, 44, 'datetime', '日期时间', 10, 10),
	(48, 3, 44, 'timestamp', '日期时间', 10, 10),
	(49, 3, 47, '10', '是', 10, 10),
	(50, 3, 47, '20', '否', 10, 10),
	(51, 3, 50, '10', '是', 10, 10),
	(52, 3, 50, '20', '否', 10, 10),
	(53, 3, 51, '10', '是', 10, 10),
	(54, 3, 51, '20', '否', 10, 10),
	(55, 3, 52, '10', '是', 10, 10),
	(56, 3, 52, '20', '否', 10, 10),
	(57, 3, 53, '10', '显示', 10, 10),
	(58, 3, 53, '20', '不显示', 10, 10),
	(59, 3, 54, '10', '固定', 10, 10),
	(60, 3, 54, '20', '不固定', 10, 10),
	(61, 3, 56, '10', '是', 10, 10),
	(62, 3, 56, '20', '否', 10, 10),
	(63, 3, 58, '10', '开启', 10, 10),
	(64, 3, 58, '20', '禁用', 10, 10),
	(65, 3, 247, '10', '是', 10, 10),
	(66, 3, 247, '20', '否', 10, 10),
	(67, 4, 61, '10', '内置', 10, 10),
	(68, 4, 61, '20', '扩展', 10, 10),
	(69, 4, 66, '10', '开启', 10, 10),
	(70, 4, 66, '20', '禁用', 10, 10),
	(71, 5, 83, 'NORMAL', '常规', 10, 10),
	(72, 5, 83, 'UNIQUE', '唯一', 10, 10),
	(73, 5, 83, 'FULLTEXT', '全文', 10, 10),
	(74, 5, 84, '10', '开启', 10, 10),
	(75, 5, 84, '20', '禁用', 10, 10),
	(76, 6, 107, '10', '开启', 10, 10),
	(77, 6, 107, '20', '禁用', 10, 10),
	(78, 7, 122, 'map-location', '定位', 10, 10),
	(79, 7, 122, 'coordinate', '坐标', 10, 10),
	(80, 7, 122, 'alarm-clock', '闹钟', 10, 10),
	(81, 7, 122, 'clock', '时钟', 10, 10),
	(82, 7, 122, 'calendar', '日历', 10, 10),
	(83, 7, 122, 'apple', '苹果', 10, 10),
	(84, 7, 122, 'pear', '梨子', 10, 10),
	(85, 7, 122, 'orange', '桔子', 10, 10),
	(86, 7, 122, 'cherry', '樱桃', 10, 10),
	(87, 7, 122, 'grape', '葡萄', 10, 10),
	(88, 7, 122, 'watermelon', '西瓜', 10, 10),
	(89, 7, 122, 'burger', '汉堡包', 10, 10),
	(90, 7, 122, 'dessert', '甜点', 10, 10),
	(91, 7, 122, 'fries', '薯条', 10, 10),
	(92, 7, 122, 'ice-cream', '冰淇淋', 10, 10),
	(93, 7, 122, 'milk-tea', '奶茶', 10, 10),
	(94, 7, 122, 'lollipop', '棒棒糖', 10, 10),
	(95, 7, 122, 'sugar', '糖果', 10, 10),
	(96, 7, 122, 'food', '食物', 10, 10),
	(97, 7, 122, 'fork-spoon', '叉勺', 10, 10),
	(98, 7, 122, 'coffee-cup', '咖啡杯', 10, 10),
	(99, 7, 122, 'goblet', '高脚杯', 10, 10),
	(100, 7, 122, 'mug', '杯子', 10, 10),
	(101, 7, 122, 'bowl', '碗', 10, 10),
	(102, 7, 122, 'arrow-left', '左箭头', 10, 10),
	(103, 7, 122, 'arrow-right', '右箭头', 10, 10),
	(104, 7, 122, 'avatar', '头像', 10, 10),
	(105, 7, 122, 'user', '用户', 10, 10),
	(106, 7, 122, 'male', '男', 10, 10),
	(107, 7, 122, 'female', '女', 10, 10),
	(108, 7, 122, 'bell', '铃', 10, 10),
	(109, 7, 122, 'basketball', '篮球', 10, 10),
	(110, 7, 122, 'bicycle', '自行车', 10, 10),
	(111, 7, 122, 'ship', '船', 10, 10),
	(112, 7, 122, 'van', '货车', 10, 10),
	(113, 7, 122, 'box', '箱子', 10, 10),
	(114, 7, 122, 'briefcase', '公文包', 10, 10),
	(115, 7, 122, 'suitcase', '手提箱', 10, 10),
	(116, 7, 122, 'brush', '刷子', 10, 10),
	(117, 7, 122, 'camera', '相机', 10, 10),
	(118, 7, 122, 'video-camera', '摄像机', 10, 10),
	(119, 7, 122, 'cellphone', '手机', 10, 10),
	(120, 7, 122, 'phone', '电话', 10, 10),
	(121, 7, 122, 'headset', '耳机', 10, 10),
	(122, 7, 122, 'watch', '手表', 10, 10),
	(123, 7, 122, 'cpu', 'CPU', 10, 10),
	(124, 7, 122, 'microphone', '麦克风', 10, 10),
	(125, 7, 122, 'monitor', '显示器', 10, 10),
	(126, 7, 122, 'mouse', '鼠标', 10, 10),
	(127, 7, 122, 'printer', '打印机', 10, 10),
	(128, 7, 122, 'picture', '图片', 10, 10),
	(129, 7, 122, 'film', '电影', 10, 10),
	(130, 7, 122, 'video-play', '播放', 10, 10),
	(131, 7, 122, 'video-pause', '暂停', 10, 10),
	(132, 7, 122, 'chat-dot-round', '聊天', 10, 10),
	(133, 7, 122, 'circle-check', '打钩', 10, 10),
	(134, 7, 122, 'circle-close', '打叉', 10, 10),
	(135, 7, 122, 'circle-plus', '圆形加号', 10, 10),
	(136, 7, 122, 'umbrella', '雨伞', 10, 10),
	(137, 7, 122, 'mostly-cloudy', '云朵', 10, 10),
	(138, 7, 122, 'wind-power', '风力', 10, 10),
	(139, 7, 122, 'lightning', '闪电', 10, 10),
	(140, 7, 122, 'sunny', '太阳', 10, 10),
	(141, 7, 122, 'moon', '月亮', 10, 10),
	(142, 7, 122, 'star', '星星', 10, 10),
	(143, 7, 122, 'money', '钞票', 10, 10),
	(144, 7, 122, 'coin', '硬币', 10, 10),
	(145, 7, 122, 'credit-card', '信用卡', 10, 10),
	(146, 7, 122, 'wallet', '钱包', 10, 10),
	(147, 7, 122, 'discount', '折扣', 10, 10),
	(148, 7, 122, 'goods', '购物袋', 10, 10),
	(149, 7, 122, 'shopping-cart', '购物车', 10, 10),
	(150, 7, 122, 'price-tag', '价格标签', 10, 10),
	(151, 7, 122, 'collection-tag', '收藏标签', 10, 10),
	(152, 7, 122, 'compass', '指南针', 10, 10),
	(153, 7, 122, 'connection', '连接', 10, 10),
	(154, 7, 122, 'link', '超链接', 10, 10),
	(155, 7, 122, 'scissor', '剪切', 10, 10),
	(156, 7, 122, 'copy-document', '复制', 10, 10),
	(157, 7, 122, 'delete', '删除', 10, 10),
	(158, 7, 122, 'rank', '移动', 10, 10),
	(159, 7, 122, 'crop', '裁切', 10, 10),
	(160, 7, 122, 'edit', '编辑', 10, 10),
	(161, 7, 122, 'filter', '过滤', 10, 10),
	(162, 7, 122, 'upload', '上传', 10, 10),
	(163, 7, 122, 'download', '下载', 10, 10),
	(164, 7, 122, 'finished', '完成', 10, 10),
	(165, 7, 122, 'document', '文档', 10, 10),
	(166, 7, 122, 'folder', '文件夹', 10, 10),
	(167, 7, 122, 'data-analysis', '数据分析', 10, 10),
	(168, 7, 122, 'histogram', '直方图', 10, 10),
	(169, 7, 122, 'trend-charts', '折线图', 10, 10),
	(170, 7, 122, 'pie-chart', '饼图', 10, 10),
	(171, 7, 122, 'flag', '旗帜', 10, 10),
	(172, 7, 122, 'full-screen', '全屏', 10, 10),
	(173, 7, 122, 'grid', '网格', 10, 10),
	(174, 7, 122, 'guide', '路标', 10, 10),
	(175, 7, 122, 'help', '帮助', 10, 10),
	(176, 7, 122, 'view', '展示', 10, 10),
	(177, 7, 122, 'hide', '隐藏', 10, 10),
	(178, 7, 122, 'list', '列表', 10, 10),
	(179, 7, 122, 'house', '房子', 10, 10),
	(180, 7, 122, 'office-building', '办公楼', 10, 10),
	(181, 7, 122, 'school', '学校', 10, 10),
	(182, 7, 122, 'shop', '商店', 10, 10),
	(183, 7, 122, 'key', '钥匙', 10, 10),
	(184, 7, 122, 'lock', '锁', 10, 10),
	(185, 7, 122, 'management', '管理', 10, 10),
	(186, 7, 122, 'magnet', '磁铁', 10, 10),
	(187, 7, 122, 'medal', '奖章', 10, 10),
	(188, 7, 122, 'menu', '菜单', 10, 10),
	(189, 7, 122, 'message-box', '消息盒子', 10, 10),
	(190, 7, 122, 'message', '信封', 10, 10),
	(191, 7, 122, 'postcard', '明信片', 10, 10),
	(192, 7, 122, 'notebook', '笔记本', 10, 10),
	(193, 7, 122, 'info-filled', '信息', 10, 10),
	(194, 7, 122, 'warning-filled', '警告', 10, 10),
	(195, 7, 122, 'notification', '通知', 10, 10),
	(196, 7, 122, 'question-filled', '问号', 10, 10),
	(197, 7, 122, 'odometer', '里程计', 10, 10),
	(198, 7, 122, 'more', '更多', 10, 10),
	(199, 7, 122, 'operation', '操作', 10, 10),
	(200, 7, 122, 'opportunity', '机会', 10, 10),
	(201, 7, 122, 'paperclip', '回形针', 10, 10),
	(202, 7, 122, 'present', '当前', 10, 10),
	(203, 7, 122, 'reading', '阅读', 10, 10),
	(204, 7, 122, 'search', '放大镜', 10, 10),
	(205, 7, 122, 'zoom-in', '放大镜+', 10, 10),
	(206, 7, 122, 'zoom-out', '放大镜-', 10, 10),
	(207, 7, 122, 'setting', '齿轮', 10, 10),
	(208, 7, 122, 'share', '分享', 10, 10),
	(209, 7, 122, 'sort', '排序', 10, 10),
	(210, 7, 122, 'stamp', '图章', 10, 10),
	(211, 7, 122, 'switch-button', '开关', 10, 10),
	(212, 7, 122, 'takeaway-box', '任务', 10, 10),
	(213, 7, 128, '10', '内置', 10, 10),
	(214, 7, 128, '20', '扩展', 10, 10),
	(215, 7, 130, '10', '开启', 10, 10),
	(216, 7, 130, '20', '禁用', 10, 10),
	(217, 8, 145, '10', '是', 10, 10),
	(218, 8, 145, '20', '否', 10, 10),
	(219, 8, 152, '10', '开启', 10, 10),
	(220, 8, 152, '20', '禁用', 10, 10),
	(221, 9, 163, 'text', '文本输入框', 10, 10),
	(222, 9, 163, 'textarea', '多行文本输入框', 10, 10),
	(223, 9, 163, 'radio', '单选框', 10, 10),
	(224, 9, 163, 'password', '密码框', 10, 10),
	(225, 9, 163, 'checkbox', '多选框', 10, 10),
	(226, 9, 163, 'input_number', '计数器', 10, 10),
	(227, 9, 163, 'select', '单选下拉框', 10, 10),
	(228, 9, 163, 'select_mul', '多选下拉框', 10, 10),
	(229, 9, 163, 'switch', '开关', 10, 10),
	(230, 9, 163, 'date', '日期日历', 10, 10),
	(231, 9, 163, 'datetime', '日期时间日历', 10, 10),
	(232, 9, 163, 'upload_image', '图片上传', 10, 10),
	(233, 9, 163, 'upload_file', '文件上传', 10, 10),
	(234, 9, 163, 'editor', '编辑器', 10, 10),
	(235, 9, 163, 'hidden', '隐藏域', 10, 10),
	(236, 9, 163, 'color_picker', '颜色选择器', 10, 10),
	(237, 9, 165, '10', '是', 10, 10),
	(238, 9, 165, '20', '否', 10, 10),
	(239, 9, 167, '10', '开启', 10, 10),
	(240, 9, 167, '20', '禁用', 10, 10),
	(241, 9, 262, '10', '是', 10, 10),
	(242, 9, 262, '20', '否', 10, 10),
	(243, 10, 183, 'require', '必填', 10, 10),
	(244, 10, 183, 'number', '纯数字(不包负数和小数点)', 10, 10),
	(245, 10, 183, 'integer', '整数', 10, 10),
	(246, 10, 183, 'float', '浮点数', 10, 10),
	(247, 10, 183, 'boolean', '布尔值', 10, 10),
	(248, 10, 183, 'email', '邮箱', 10, 10),
	(249, 10, 183, 'array', '数组', 10, 10),
	(250, 10, 183, 'accepted', '是否为(yes,on,或是1)', 10, 10),
	(251, 10, 183, 'date', '日期', 10, 10),
	(252, 10, 183, 'alpha', '纯字母', 10, 10),
	(253, 10, 183, 'alphaNum', '字母和数字', 10, 10),
	(254, 10, 183, 'alphaDash', '字母和数字，下划线_及破折号-', 10, 10),
	(255, 10, 183, 'chs', '纯汉字', 10, 10),
	(256, 10, 183, 'chsAlpha', '汉字、字母', 10, 10),
	(257, 10, 183, 'chsAlphaNum', '汉字、字母和数字', 10, 10),
	(258, 10, 183, 'chsDash', '汉字、字母、数字和下划线_及破折号-', 10, 10),
	(259, 10, 183, 'cntrl', '换行、缩进、空格', 10, 10),
	(260, 10, 183, 'graph', '可打印字符(空格除外)', 10, 10),
	(261, 10, 183, 'print', '可打印字符(包括空格)', 10, 10),
	(262, 10, 183, 'lower', '小写字符', 10, 10),
	(263, 10, 183, 'upper', '大写字符', 10, 10),
	(264, 10, 183, 'space', '空白字符(包括缩进，垂直制表符，换行符，回车和换页字符)', 10, 10),
	(265, 10, 183, 'xdigit', '十六进制字符串', 10, 10),
	(266, 10, 183, 'activeUrl', '域名或者IP', 10, 10),
	(267, 10, 183, 'url', 'URL地址', 10, 10),
	(268, 10, 183, 'ip', 'IP地址', 10, 10),
	(269, 10, 183, 'dateFormat', '指定格式的日期', 10, 10),
	(270, 10, 183, 'mobile', '手机号码', 10, 10),
	(271, 10, 183, 'idCard', '身份证号码', 10, 10),
	(272, 10, 183, 'macAddr', 'MAC地址', 10, 10),
	(273, 10, 183, 'zip', '邮政编码', 10, 10),
	(274, 10, 183, 'in', '在某个范围', 10, 10),
	(275, 10, 183, 'notIn', '不在某个范围', 10, 10),
	(276, 10, 183, 'between', '在某个区间', 10, 10),
	(277, 10, 183, 'notBetween', '不在某个范围', 10, 10),
	(278, 10, 183, 'length', '长度是否在某个范围', 10, 10),
	(279, 10, 183, 'max', '最大长度', 10, 10),
	(280, 10, 183, 'min', '最小长度', 10, 10),
	(281, 10, 183, 'after', '在某个日期之后', 10, 10),
	(282, 10, 183, 'before', '在某个日期之前', 10, 10),
	(283, 10, 183, 'expire', '在某个有效日期之内', 10, 10),
	(284, 10, 183, 'allowIp', 'IP是否在某个范围(多个IP用逗号分隔)', 10, 10),
	(285, 10, 183, 'denyIp', 'IP是否禁止(多个IP用逗号分隔)', 10, 10),
	(286, 10, 183, 'confirm', '和另外一个字段的值一致', 10, 10),
	(287, 10, 183, 'different', '和另外一个字段的值不一致', 10, 10),
	(288, 10, 183, '=', '等于某个值', 10, 10),
	(289, 10, 183, '>=', '大于等于某个值', 10, 10),
	(290, 10, 183, '>', '大于某个值', 10, 10),
	(291, 10, 183, '<=', '小于等于某个值', 10, 10),
	(292, 10, 183, '<', '小于某个值', 10, 10),
	(293, 10, 183, 'filter', '支持使用filter_var进行验证', 10, 10),
	(294, 10, 183, 'regex', '正则验证', 10, 10),
	(295, 10, 183, 'file', '文件', 10, 10),
	(296, 10, 183, 'image', '图像文件', 10, 10),
	(297, 10, 183, 'fileExt', '上传文件后缀', 10, 10),
	(298, 10, 183, 'fileMime', '上传文件类型', 10, 10),
	(299, 10, 183, 'fileSize', '上传文件大小', 10, 10),
	(300, 10, 183, 'token', '表单令牌', 10, 10),
	(301, 10, 183, 'unique', '请求的字段值是否为唯一', 10, 10),
	(302, 10, 183, 'requireIf', '某个字段的值等于某个值的时候必须', 10, 10),
	(303, 10, 183, 'requireWith', '某个字段有值的时候必须', 10, 10),
	(304, 10, 183, 'requireWithout', '某个字段没有值的时候必须', 10, 10),
	(305, 10, 183, 'requireCallback', '某个callable为真的时候字段必须', 10, 10),
	(306, 10, 186, '10', '开启', 10, 10),
	(307, 10, 186, '20', '禁用', 10, 10),
	(308, 11, 206, '10', '开启', 10, 10),
	(309, 11, 206, '20', '禁用', 10, 10),
	(310, 12, 225, '10', '开启', 10, 10),
	(311, 12, 225, '20', '禁用', 10, 10),
	(312, 14, 242, '10', '是', 10, 10),
	(313, 14, 242, '20', '否', 10, 10),
	(314, 14, 243, '10', '是', 10, 10),
	(315, 14, 243, '20', '否', 10, 10),
	(316, 14, 245, '10', '内置', 10, 10),
	(317, 14, 245, '20', '扩展', 10, 10),
	(318, 14, 246, '10', '开启', 10, 10),
	(319, 14, 246, '20', '禁用', 10, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_relation")
	query = fmt.Sprintf(`INSERT INTO %s(id, model_id, model_field_id, relation_model_id, relation_field_id, relation_show_field_id, relation_filter, status) VALUES 
	(1, 1, 2, 14, 240, '241', '', 10),
	(2, 1, 6, 2, 20, '21', '', 10),
	(3, 1, 7, 3, 40, '41,42', '', 10),
	(4, 2, 23, 1, 1, '3,4', '', 10),
	(5, 3, 43, 1, 1, '3,4', '', 10),
	(6, 4, 62, 1, 1, '3,4', '', 10),
	(7, 4, 63, 3, 40, '41,42', '', 10),
	(8, 5, 81, 1, 1, '3,4', '', 10),
	(9, 5, 82, 3, 40, '41,42', '', 10),
	(10, 6, 101, 1, 1, '3,4', '', 10),
	(11, 6, 102, 3, 40, '41,42', '', 10),
	(12, 6, 103, 1, 1, '3,4', '', 10),
	(13, 6, 104, 3, 40, '41,42', '', 10),
	(14, 6, 105, 3, 40, '41,42', '', 10),
	(15, 7, 127, 1, 1, '3,4', '', 10),
	(16, 7, 124, 14, 240, '241', '', 10),
	(17, 9, 161, 1, 1, '3,4', '', 10),
	(18, 9, 162, 3, 40, '41,42', '', 10),
	(19, 10, 181, 1, 1, '3,4', '', 10),
	(20, 10, 182, 9, 160, '161,162,163', '', 10),
	(21, 12, 221, 1, 1, '3,4', '', 10),
	(22, 12, 222, 3, 40, '41,42', '', 10),
	(23, 12, 223, 3, 40, '41,42', '', 10),
	(24, 12, 224, 2, 20, '21', '', 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_action")
	query = fmt.Sprintf(`INSERT INTO %s(id, label, api_path, model_id, action_type, status) VALUES 
	(1, '模型管理列表', '/vuecmf/model_config/', 1, 'list', 10),
	(2, '保存模型', '/vuecmf/model_config/save', 1, 'save', 10),
	(3, '删除模型', '/vuecmf/model_config/delete', 1, 'delete', 10),
	(4, '批量保存模型', '/vuecmf/model_config/save_all', 1, 'save_all', 10),
	(5, '批量删除模型', '/vuecmf/model_config/delete_batch', 1, 'delete_batch', 10),
	(6, '模型下拉列表', '/vuecmf/model_config/dropdown', 1, 'dropdown', 10),
	(20, '模型动作管理列表', '/vuecmf/model_action/', 2, 'list', 10),
	(21, '保存模型动作', '/vuecmf/model_action/save', 2, 'save', 10),
	(22, '删除模型动作', '/vuecmf/model_action/delete', 2, 'delete', 10),
	(23, '动作下拉列表', '/vuecmf/model_action/dropdown', 2, 'dropdown', 10),
	(24, '批量保存模型动作', '/vuecmf/model_action/save_all', 2, 'save_all', 10),
	(25, '批量删除模型动作', '/vuecmf/model_action/delete_batch', 2, 'delete_batch', 10),
	(40, '模型字段管理列表', '/vuecmf/model_field/', 3, 'list', 10),
	(41, '保存模型字段', '/vuecmf/model_field/save', 3, 'save', 10),
	(42, '删除模型字段', '/vuecmf/model_field/delete', 3, 'delete', 10),
	(43, '字段下拉列表', '/vuecmf/model_field/dropdown', 3, 'dropdown', 10),
	(44, '批量保存模型字段', '/vuecmf/model_field/save_all', 3, 'save_all', 10),
	(45, '批量删除模型字段', '/vuecmf/model_field/delete_batch', 3, 'delete_batch', 10),
	(60, '字段选项管理列表', '/vuecmf/field_option/', 4, 'list', 10),
	(61, '保存字段选项', '/vuecmf/field_option/save', 4, 'save', 10),
	(62, '删除字段选项', '/vuecmf/field_option/delete', 4, 'delete', 10),
	(63, '批量保存字段选项', '/vuecmf/field_option/save_all', 4, 'save_all', 10),
	(64, '批量删除字段选项', '/vuecmf/field_option/delete_batch', 4, 'delete_batch', 10),
	(65, '字段选项下拉列表', '/vuecmf/field_option/dropdown', 4, 'dropdown', 10),
	(80, '模型索引管理列表', '/vuecmf/model_index/', 5, 'list', 10),
	(81, '保存模型索引', '/vuecmf/model_index/save', 5, 'save', 10),
	(82, '删除模型索引', '/vuecmf/model_index/delete', 5, 'delete', 10),
	(83, '批量保存模型索引', '/vuecmf/model_index/save_all', 5, 'save_all', 10),
	(84, '批量删除模型索引', '/vuecmf/model_index/delete_batch', 5, 'delete_batch', 10),
	(100, '模型关联管理列表', '/vuecmf/model_relation/', 6, 'list', 10),
	(101, '保存模型关联', '/vuecmf/model_relation/save', 6, 'save', 10),
	(102, '删除模型关联', '/vuecmf/model_relation/delete', 6, 'delete', 10),
	(103, '批量保存模型关联', '/vuecmf/model_relation/save_all', 6, 'save_all', 10),
	(104, '批量删除模型关联', '/vuecmf/model_relation/delete_batch', 6, 'delete_batch', 10),
	(120, '菜单管理列表', '/vuecmf/menu/', 7, 'list', 10),
	(121, '保存菜单', '/vuecmf/menu/save', 7, 'save', 10),
	(122, '删除菜单', '/vuecmf/menu/delete', 7, 'delete', 10),
	(123, '导航菜单', '/vuecmf/menu/nav', 7, 'nav', 10),
	(124, '批量保存菜单', '/vuecmf/menu/save_all', 7, 'save_all', 10),
	(125, '批量删除菜单', '/vuecmf/menu/delete_batch', 7, 'delete_batch', 10),
	(140, '管理员列表', '/vuecmf/admin/', 8, 'list', 10),
	(141, '保存管理员', '/vuecmf/admin/save', 8, 'save', 10),
	(142, '删除管理员', '/vuecmf/admin/delete', 8, 'delete', 10),
	(143, '管理员详情', '/vuecmf/admin/detail', 8, 'detail', 10),
	(144, '获取动作列表', '/vuecmf/model_action/get_action_list', 8, 'action_list', 10),
	(145, '分配角色', '/vuecmf/admin/add_role', 8, 'assign_role', 10),
	(146, '登录后台', '/vuecmf/admin/login', 8, 'login', 10),
	(147, '退出系统', '/vuecmf/admin/logout', 8, 'logout', 10),
	(148, '批量保存管理员', '/vuecmf/admin/save_all', 8, 'save_all', 10),
	(149, '批量删除管理员', '/vuecmf/admin/delete_batch', 8, 'delete_batch', 10),
	(150, '获取所有角色', '/vuecmf/admin/get_all_roles', 8, 'get_all_roles', 10),
	(151, '获取用户的角色', '/vuecmf/admin/get_roles', 8, 'get_roles', 10),
	(152, '设置用户权限', '/vuecmf/admin/set_user_permission', 8, 'set_user_permission', 10),
	(153, '获取用户权限', '/vuecmf/admin/get_user_permission', 8, 'get_user_permission', 10),
	(180, '模型表单管理列表', '/vuecmf/model_form/', 9, 'list', 10),
	(181, '保存模型表单', '/vuecmf/model_form/save', 9, 'save', 10),
	(182, '删除模型表单', '/vuecmf/model_form/delete', 9, 'delete', 10),
	(183, '表单下拉列表', '/vuecmf/model_form/dropdown', 9, 'dropdown', 10),
	(184, '批量保存模型表单', '/vuecmf/model_form/save_all', 9, 'save_all', 10),
	(185, '批量删除模型表单', '/vuecmf/model_form/delete_batch', 9, 'delete_batch', 10),
	(200, '模型表单验证管理列表', '/vuecmf/model_form_rules/', 10, 'list', 10),
	(201, '保存模型表单验证', '/vuecmf/model_form_rules/save', 10, 'save', 10),
	(202, '删除模型表单验证', '/vuecmf/model_form_rules/delete', 10, 'delete', 10),
	(203, '批量保存模型表单验证', '/vuecmf/model_form_rules/save_all', 10, 'save_all', 10),
	(204, '批量删除模型表单验证', '/vuecmf/model_form_rules/delete_batch', 10, 'delete_batch', 10),
	(220, '角色管理列表', '/vuecmf/roles/', 11, 'list', 10),
	(221, '保存角色', '/vuecmf/roles/save', 11, 'save', 10),
	(222, '删除角色', '/vuecmf/roles/delete', 11, 'delete', 10),
	(223, '批量保存角色', '/vuecmf/roles/save_all', 11, 'save_all', 10),
	(224, '获取动作列表', '/vuecmf/model_action/get_action_list', 11, 'action_list', 10),
	(225, '批量删除角色', '/vuecmf/roles/delete_batch', 11, 'delete_batch', 10),
	(226, '批量分配用户', '/vuecmf/roles/add_users', 11, 'assign_users', 10),
	(227, '批量删除用户', '/vuecmf/roles/del_users', 11, 'del_users', 10),
	(228, '设置角色权限', '/vuecmf/roles/add_permission', 11, 'add_permission', 10),
	(229, '删除角色权限', '/vuecmf/roles/del_permission', 11, 'del_permission', 10),
	(230, '获取角色下所有用户', '/vuecmf/roles/get_users', 11, 'get_users', 10),
	(231, '获取角色下所有权限', '/vuecmf/roles/get_permission', 11, 'get_permission', 10),
	(232, '获取所有用户', '/vuecmf/roles/get_all_users', 11, 'get_all_users', 10),
	(260, '模型联动设置列表', '/vuecmf/model_form_linkage/', 12, 'list', 10),
	(261, '保存模型联动设置', '/vuecmf/model_form_linkage/save', 12, 'save', 10),
	(262, '删除模型联动设置', '/vuecmf/model_form_linkage/delete', 12, 'delete', 10),
	(263, '批量保存模型联动设置', '/vuecmf/model_form_linkage/save_all', 12, 'save_all', 10),
	(264, '批量删除模型联动设置', '/vuecmf/model_form_linkage/delete_batch', 12, 'delete_batch', 10),
	(280, '文件上传', '/vuecmf/upload/', 13, 'upload', 10),
	(300, '应用管理列表', '/vuecmf/app_config/', 14, 'list', 10),
	(301, '保存应用', '/vuecmf/app_config/save', 14, 'save', 10),
	(302, '删除应用', '/vuecmf/app_config/delete', 14, 'delete', 10),
	(303, '应用下拉列表', '/vuecmf/app_config/dropdown', 14, 'dropdown', 10),
	(304, '批量保存应用', '/vuecmf/app_config/save_all', 14, 'save_all', 10),
	(305, '批量删除应用', '/vuecmf/app_config/delete_batch', 14, 'delete_batch', 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_form")
	query = fmt.Sprintf(`INSERT INTO %s(id, model_id, model_field_id, type, default_value, is_disabled, placeholder, is_edit, sort_num, status) VALUES 
	(1, 1, 2, 'select', '', 20, '', 10, 1, 10),
	(2, 1, 3, 'text', '', 20, '', 10, 2, 10),
	(3, 1, 4, 'text', '', 20, '', 10, 3, 10),
	(4, 1, 5, 'select', 'template/content/List', 20, '', 10, 4, 10),
	(5, 1, 6, 'select', '', 20, '', 10, 5, 10),
	(6, 1, 7, 'select_mul', '', 20, '', 10, 6, 10),
	(7, 1, 9, 'radio', '20', 20, '', 10, 7, 10),
	(8, 1, 10, 'textarea', '', 20, '', 10, 8, 10),
	(9, 1, 11, 'radio', '10', 20, '', 10, 9, 10),
	(20, 2, 23, 'select', '', 10, '', 10, 20, 10),
	(21, 2, 21, 'text', '', 20, '', 10, 21, 10),
	(22, 2, 22, 'text', '', 20, '', 10, 22, 10),
	(23, 2, 24, 'select', '', 20, '', 10, 23, 10),
	(24, 2, 25, 'radio', '10', 20, '', 10, 24, 10),
	(40, 3, 41, 'text', '', 20, '', 10, 40, 10),
	(41, 3, 42, 'text', '', 20, '', 10, 41, 10),
	(42, 3, 43, 'select', '', 10, '', 10, 42, 10),
	(43, 3, 44, 'select', '', 20, '', 10, 43, 10),
	(44, 3, 45, 'input_number', '', 20, '', 10, 44, 10),
	(45, 3, 46, 'input_number', '0', 20, '', 10, 45, 10),
	(46, 3, 47, 'radio', '10', 20, '', 10, 46, 10),
	(47, 3, 49, 'text', '', 20, '', 10, 47, 10),
	(48, 3, 48, 'textarea', '', 20, '', 10, 48, 10),
	(49, 3, 50, 'radio', '20', 20, '', 10, 49, 10),
	(50, 3, 51, 'radio', '20', 20, '', 10, 50, 10),
	(51, 3, 52, 'radio', '10', 20, '', 10, 51, 10),
	(52, 3, 53, 'radio', '10', 20, '', 10, 52, 10),
	(53, 3, 54, 'radio', '20', 20, '', 10, 53, 10),
	(54, 3, 55, 'input_number', '150', 20, '', 10, 54, 10),
	(55, 3, 56, 'radio', '10', 20, '', 10, 55, 10),
	(56, 3, 57, 'input_number', '0', 20, '', 10, 56, 10),
	(57, 3, 58, 'radio', '10', 20, '', 10, 57, 10),
	(80, 4, 62, 'select', '', 10, '', 10, 80, 10),
	(81, 4, 63, 'select', '', 10, '', 10, 81, 10),
	(82, 4, 64, 'text', '', 20, '', 10, 82, 10),
	(83, 4, 65, 'text', '', 20, '', 10, 83, 10),
	(84, 4, 66, 'radio', '10', 20, '', 10, 84, 10),
	(100, 5, 81, 'select', '', 10, '', 10, 100, 10),
	(101, 5, 82, 'select_mul', '', 20, '', 10, 101, 10),
	(102, 5, 83, 'select', 'NORMAL', 20, '', 10, 102, 10),
	(103, 5, 84, 'radio', '10', 20, '', 10, 103, 10),
	(120, 6, 101, 'select', '', 10, '', 10, 120, 10),
	(121, 6, 102, 'select', '', 10, '', 10, 121, 10),
	(122, 6, 103, 'select', '', 20, '', 10, 122, 10),
	(123, 6, 104, 'select', '', 20, '', 10, 123, 10),
	(124, 6, 105, 'select_mul', '', 20, '', 10, 124, 10),
	(125, 6, 106, 'text', '', 20, '', 10, 125, 10),
	(126, 6, 107, 'radio', '10', 20, '', 10, 126, 10),
	(140, 7, 121, 'text', '', 20, '', 10, 140, 10),
	(141, 7, 122, 'select', '', 20, '', 10, 141, 10),
	(142, 7, 123, 'select', '', 20, '', 10, 142, 10),
	(143, 7, 124, 'select', '', 20, '', 10, 143, 10),
	(144, 7, 127, 'select', '', 20, '', 10, 144, 10),
	(145, 7, 129, 'input_number', '0', 20, '', 10, 145, 10),
	(146, 7, 130, 'radio', '10', 20, '', 10, 146, 10),
	(160, 8, 141, 'text', '', 20, '用户名长度为4到32个字符', 20, 160, 10),
	(161, 8, 142, 'password', '', 20, '', 10, 161, 10),
	(162, 8, 143, 'text', '', 20, '', 10, 162, 10),
	(163, 8, 144, 'text', '', 20, '', 10, 163, 10),
	(164, 8, 145, 'radio', '20', 20, '', 10, 164, 10),
	(180, 9, 161, 'select', '', 10, '', 10, 180, 10),
	(181, 9, 162, 'select', '', 20, '', 10, 181, 10),
	(182, 9, 163, 'select', '', 20, '', 10, 182, 10),
	(183, 9, 164, 'text', '', 20, '', 10, 183, 10),
	(184, 9, 165, 'radio', '20', 20, '', 10, 184, 10),
	(185, 9, 166, 'input_number', '0', 20, '', 10, 185, 10),
	(186, 9, 167, 'radio', '10', 20, '', 10, 186, 10),
	(200, 10, 181, 'select', '', 10, '', 10, 200, 10),
	(201, 10, 182, 'select', '', 10, '', 10, 201, 10),
	(202, 10, 183, 'select', '', 20, '', 10, 202, 10),
	(203, 10, 184, 'text', '', 20, '', 10, 203, 10),
	(204, 10, 185, 'text', '', 20, '', 10, 204, 10),
	(205, 10, 186, 'radio', '10', 20, '', 10, 205, 10),
	(220, 11, 201, 'text', '', 20, '', 10, 220, 10),
	(221, 11, 203, 'select', '', 20, '', 10, 221, 10),
	(222, 11, 205, 'textarea', '', 20, '', 10, 222, 10),
	(223, 11, 206, 'radio', '10', 20, '', 10, 223, 10),
	(240, 12, 221, 'select', '', 10, '', 10, 240, 10),
	(241, 12, 222, 'select', '', 10, '', 10, 241, 10),
	(242, 12, 223, 'select', '', 20, '', 10, 242, 10),
	(243, 12, 224, 'select', '', 20, '', 10, 243, 10),
	(244, 12, 225, 'radio', '10', 20, '', 10, 244, 10),
	(260, 14, 241, 'text', '', 20, '', 10, 260, 10),
	(261, 14, 242, 'radio', '10', 20, '', 10, 261, 10),
	(262, 14, 243, 'radio', '10', 20, '', 10, 262, 10),
	(263, 14, 244, 'textarea', '', 20, '', 10, 263, 10),
	(264, 14, 245, 'radio', '20', 20, '', 10, 264, 10),
	(265, 3, 247, 'radio', '20', 20, '', 10, 55, 10),
	(273, 9, 261, 'text', '', 20, '', 10, 185, 10),
	(274, 9, 262, 'radio', '10', 20, '', 10, 186, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_form_rules")
	query = fmt.Sprintf(`INSERT INTO %s(id, model_id, model_form_id, rule_type, rule_value, error_tips, status) VALUES 
	(1, 1, 1, 'require', '', '请选择', 10),
	(2, 1, 2, 'require', '', '表名必填', 10),
	(3, 1, 3, 'require', '', '模型标签必填', 10),
	(4, 1, 4, 'require', '', '请选择', 10),
	(5, 2, 20, 'require', '', '请选择', 10),
	(6, 2, 21, 'require', '', '动作标签必填', 10),
	(7, 2, 22, 'require', '', '后端请求地址必填', 10),
	(8, 2, 23, 'require', '', '请选择', 10),
	(9, 2, 24, 'require', '', '请选择', 10),
	(10, 3, 40, 'require', '', '字段名称必填', 10),
	(11, 3, 41, 'require', '', '字段中文名必填', 10),
	(12, 3, 42, 'require', '', '请选择', 10),
	(13, 3, 43, 'require', '', '请选择', 10),
	(14, 3, 44, 'number', '', '请输入数字', 10),
	(15, 4, 80, 'require', '', '请选择', 10),
	(16, 4, 81, 'require', '', '请选择', 10),
	(17, 4, 82, 'require', '', '选项值必填', 10),
	(18, 4, 83, 'require', '', '选项标签必填', 10),
	(19, 5, 100, 'require', '', '请选择', 10),
	(20, 5, 101, 'require', '', '请选择', 10),
	(21, 5, 102, 'require', '', '请选择', 10),
	(22, 6, 120, 'require', '', '请选择', 10),
	(23, 6, 121, 'require', '', '请选择', 10),
	(24, 6, 122, 'require', '', '请选择', 10),
	(25, 6, 123, 'require', '', '请选择', 10),
	(26, 6, 124, 'require', '', '请选择', 10),
	(27, 7, 140, 'require', '', '菜单标题必填', 10),
	(28, 8, 160, 'require', '', '用户名必填', 10),
	(29, 8, 160, 'length', '4,32', '用户名长度为4到32个字符', 10),
	(30, 8, 162, 'require', '', '邮箱必填', 10),
	(31, 8, 162, 'email', '', '邮箱输入有误', 10),
	(32, 8, 163, 'require', '', '手机必填', 10),
	(33, 8, 163, 'mobile', '', '手机输入有误', 10),
	(34, 9, 180, 'require', '', '请选择', 10),
	(35, 9, 181, 'require', '', '请选择', 10),
	(36, 9, 182, 'require', '', '请选择', 10),
	(37, 10, 200, 'require', '', '请选择', 10),
	(38, 10, 201, 'require', '', '请选择', 10),
	(39, 10, 202, 'require', '', '请选择', 10),
	(40, 11, 220, 'require', '', '角色名称必填', 10),
	(41, 12, 240, 'require', '', '请选择', 10),
	(42, 12, 241, 'require', '', '请选择', 10),
	(43, 12, 242, 'require', '', '请选择', 10),
	(44, 12, 243, 'require', '', '请选择', 10),
	(45, 14, 260, 'require', '', '应用名称必填', 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("model_form_linkage")
	query = fmt.Sprintf(`INSERT INTO %s(id, model_id, model_field_id, linkage_field_id, linkage_action_id, status) VALUES 
	(1, 1, 3, 6, 23, 10),
	(2, 1, 3, 7, 43, 10),
	(3, 5, 81, 82, 43, 10),
	(4, 6, 103, 104, 43, 10),
	(5, 6, 103, 105, 43, 10),
	(6, 7, 124, 127, 6, 10),
	(7, 9, 161, 162, 43, 10),
	(8, 9, 163, 164, 65, 10),
	(9, 12, 223, 224, 23, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	//password = 123456
	tableName = db.NamingStrategy.TableName("admin")
	query = fmt.Sprintf(`INSERT INTO %s(id, username, password, email, mobile, is_super, reg_time, reg_ip, last_login_time, last_login_ip, update_time, token, pid, status) VALUES (1, 'vuecmf', '$2a$10$/OGrqqU4vJomU475crwBM.wi3380HsDY3RWnq1OyZaM7RRQvzZMeG', '2278667823@qq.com', '18988888888', 10, '2024-04-06 23:35:08', '', '2024-10-23 00:03:45', '::1', '2024-10-23 00:03:45', '2966e9ff9c1a96efc3f407e9df81e611', 0, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	tableName = db.NamingStrategy.TableName("menu")
	query = fmt.Sprintf(`INSERT INTO %s(id, title, icon, pid, id_path, path_name, model_id, app_id, type, sort_num, status) VALUES 
	(1, '系统管理', 'setting', 0, '', '', 0, 1, 10, 999999, 10),
	(2, '系统授权', 'lock', 1, 'm1', '系统管理,系统授权', 0, 1, 10, 2, 10),
	(3, '管理员', 'user', 2, 'm1,m2', '系统管理,系统授权,管理员', 8, 1, 10, 3, 10),
	(4, '角色', 'document', 2, 'm1,m2', '系统管理,系统授权,角色', 11, 1, 10, 4, 10),
	(5, '应用管理', 'folder', 1, 'm1', '系统管理,应用管理', 14, 1, 10, 5, 10),
	(6, '模型配置', 'document-copy', 1, 'm1', '系统管理,模型配置', 1, 1, 10, 6, 10),
	(7, '菜单配置', 'notebook', 1, 'm1', '系统管理,菜单配置', 7, 1, 10, 7, 10);`, tableName)
	db.Exec(query)
	fmt.Println("表" + tableName + "的初始数据写入完成！")

	currentDate, _ := strconv.Atoi(time.Now().Format("20060102150304"))

	db.Create(&model.Migrations{
		Version:       int64(currentDate),
		MigrationName: migrationName,
		StartTime:     startTime,
		EndTime:       time.Now(),
	})

	return nil
}

// 升级、回滚列表
type sqlList struct {
	Up   map[string][]string `json:"up"`
	Down map[string][]string `json:"down"`
}

// 迁移文件信息
type migrationInfo struct {
	FileName      string
	Path          string
	Version       int64
	MigrationName string
}

// up 数据库升级
func up() error {
	db := app.Connect(app.Config().Env)
	//读取包含sql语句升级的JSON文件
	miDbList, err := readDbFile(db, 0)
	if err != nil {
		return err
	}

	if len(miDbList) == 0 {
		return errors.New("当前数据库已是最新版本")
	}

	for _, miDb := range miDbList {
		startTime := time.Now()

		//读取升级SQL
		err = execSql(db, miDb, "up")
		if err != nil {
			break
		}

		//写入升级日志
		db.Create(&model.Migrations{
			Version:       miDb.Version,
			MigrationName: miDb.MigrationName,
			StartTime:     startTime,
			EndTime:       time.Now(),
		})
	}

	return err
}

// down 数据库回滚
func down(rollbackVer int) error {
	db := app.Connect(app.Config().Env)
	//读取包含回滚sql语句的JSON文件
	miDbList, err := readDbFile(db, rollbackVer)
	if err != nil {
		return err
	}

	if len(miDbList) == 0 {
		return errors.New("当前数据库没有可回滚的版本")
	}

	for _, miDb := range miDbList {
		//读取回滚SQL
		err = execSql(db, miDb, "down")
		if err != nil {
			break
		}

		//清除升级日志
		db.Delete(&model.Migrations{}, &model.Migrations{
			Version:       miDb.Version,
			MigrationName: miDb.MigrationName,
		})
	}

	return err
}

// readDbFile 读取需要升级的数据库json文件
func readDbFile(db *gorm.DB, rollbackVer int) ([]*migrationInfo, error) {
	path := "migrations"
	var mi []*migrationInfo

	var oldMigrations []model.Migrations
	//若指定了回滚版本，则取出指定回滚版本之后的历史版本
	if rollbackVer > 0 {
		db.Table(db.NamingStrategy.TableName("migrations")).Select("*").
			Where("version >= ?", rollbackVer).Order("version desc").Find(&oldMigrations)

		for _, miInfo := range oldMigrations {
			fileName := strconv.Itoa(int(miInfo.Version)) + "_" + miInfo.MigrationName + ".json"

			mi = append(mi, &migrationInfo{
				FileName:      fileName,
				Path:          path,
				Version:       miInfo.Version,
				MigrationName: miInfo.MigrationName,
			})
		}

		return mi, nil
	}

	rd, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, errors.New("读取migrations目录失败！" + err.Error())
	}

	for _, file := range rd {
		if !file.IsDir() {
			fileName := file.Name()
			arr := strings.Split(fileName, ".")
			arr = strings.Split(arr[0], "_")
			if len(arr) != 2 {
				err = errors.New("数据库升级文件" + fileName + "的文件名格式错误")
				break
			}

			version, _ := strconv.Atoi(arr[0])

			if rollbackVer == 0 {
				var ver int
				db.Table(db.NamingStrategy.TableName("migrations")).Select("version").
					Where("version = ?", version).Find(&ver)
				if ver == 0 {
					mi = append(mi, &migrationInfo{
						FileName:      fileName,
						Path:          path,
						Version:       int64(version),
						MigrationName: arr[1],
					})
				}
			}
		}
	}

	return mi, err
}

// execSql 执行数据库迁移文件中SQL
func execSql(db *gorm.DB, miDb *migrationInfo, action string) error {
	//读取升级文件
	jsonData, err := os.Open(miDb.Path + "/" + miDb.FileName)
	if err != nil {
		return errors.New("读取数据库升级文件" + miDb.FileName + "失败！" + err.Error())
	}

	var sqlData sqlList
	err = json.NewDecoder(jsonData).Decode(&sqlData)

	if err != nil {
		return errors.New("数据库升级文件" + miDb.FileName + "解析失败！" + err.Error())
	}

	exeSqlList := sqlData.Up
	if action == "down" {
		exeSqlList = sqlData.Down
	}

	//执行SQL
	if len(exeSqlList) > 0 {
		for tableName, sqlArr := range exeSqlList {
			for _, sql := range sqlArr {
				sql = strings.Replace(sql, "[table]", db.NamingStrategy.TableName(tableName), -1)
				db.Exec(sql)
			}
		}
	}

	return nil
}

// createTable 创建表
func createTable(db *gorm.DB, tableName string, model interface{}, comment string) error {
	err := db.Migrator().DropTable(&model)
	if err != nil {
		return errors.New("删除历史表" + db.NamingStrategy.TableName(tableName) + "失败:" + err.Error())
	}
	err = db.Set("gorm:table_options", "ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='"+comment+"'").
		AutoMigrate(&model)
	if err != nil {
		return errors.New("创建表" + db.NamingStrategy.TableName(tableName) + "失败:" + err.Error())
	}
	fmt.Println("表" + db.NamingStrategy.TableName(tableName) + "创建完成！")
	return err
}
